//
//  NSString+Extend.m
//  Test
//
//  Created by lizq on 15/11/20.
//  Copyright © 2015年 lizq. All rights reserved.
//

#import "NSString+Extend.h"

@implementation NSString (Extend)

//正则匹配
- (BOOL)matchRegex:(NSString *)regex {
    if(!regex || [regex length] == 0) {
        return NO;
    }
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    BOOL macth = [predicate evaluateWithObject:self];
    return macth;
}

- (BOOL)isMobileTel {
    NSString *regex = @"^(1[34578])\\d{9}$";
    return [self matchRegex:regex];
}

- (BOOL)isHomeTel {
    NSString *regex = @"^(0[0-9])\\d{9}$";
    return [self matchRegex:regex];
}

- (BOOL)isTel {
    return [self isMobileTel] || [self isHomeTel];
}

- (NSInteger)UTF8Length {
    NSUInteger asciiLength = 0;
    for (NSUInteger i = 0; i < self.length; i++) {
        unichar uc = [self characterAtIndex: i];
        asciiLength += isascii(uc) ? 1 : 2;
    }
    NSUInteger utf8Length = asciiLength / 2;
    if(asciiLength % 2) {
        utf8Length++;
    }
    return utf8Length;
}

- (NSInteger)ASCIILength {
    NSUInteger asciiLength = 0;
    for (NSUInteger i = 0; i < self.length; i++) {
        unichar uc = [self characterAtIndex: i];
        asciiLength += isascii(uc) ? 1 : 2;
    }
    return asciiLength;
}


@end
